﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using PropertyManagement.WebUI.Models;
using PropertyManagement.Business.AbstractService;
using PropertyManagement.Business.Service;
using PropertyManagement.Domain.Entity;
using System.Web.Mvc;
using PropertyManagement.Business.Common;
using PropertyManagement.WebUI.Infrastructure.Attribte;

namespace PropertyManagement.WebUI.Controllers
{
    public class CustomerAccountController : Controller
    {
        ICustomerService customerService;
        public CustomerAccountController()
        {
            customerService = new CustomerService();
        }

        public ActionResult LogOn()
        {
            return View();
        }

        public ActionResult LogOff()
        {
            Session[Constant.SESSION_CUSTOMER] = null;
            return RedirectToAction("LogOn");
        }

        [HttpPost]
        public ActionResult LogOn(LoginModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                if (!String.IsNullOrEmpty(returnUrl))
                {
                    return Redirect(returnUrl);
                }
                else
                {
                    Customer customer = new Customer
                    {
                        Email = model.UserName,
                        Password = model.Password
                    };

                    customer = customerService.GetLogInCustomer(customer);
                    if (customer != null)
                    {
                        Session[Constant.SESSION_CUSTOMER] =  customer;
                        return RedirectToAction("Index", "Home");
                    }
                    else
                    {
                        ModelState.AddModelError("","Login failed!!!");
                    }
                }
            }
            return View(model);
        }

        public ActionResult Register()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Register(CustomerRegisterModel model)
        {
            if (ModelState.IsValid)
            {
                Customer customer = new Customer()
                {
                    Id = 1,
                    FirstName = model.FirstName,
                    LastName = model.LastName,
                    Email = model.Email,
                    CreatedDate = DateTime.Now,
                    Active = true,
                    Rating = 0,
                    MobilePhoneNo = model.MobileNo,
                    HousePhoneNo = model.HousePhoneNo,
                    OfficePhoneNo = model.OfficePhoneNo,
                    Password = model.Password,
                    PasswordSoalt = "vxcv",
                    ReferenceNo = "refno",
                    

                };

                bool success = customerService.CreateCustomer(customer);
                if (success)
                {
                    return RedirectToAction("Index", "Home");
                }
                else
                {
                    ModelState.AddModelError("", "Unable to Save the record");
                }
            }
            return View(model);
        }

        public ActionResult MyAccount()
        {
            return View();
        }

        [RequiresAuthentication]
        public ActionResult EditProfile()
        {
            Customer customer = (Customer)Session[Constant.SESSION_CUSTOMER];
            if (customer != null)
            {
                EditProfile editProfileModel = new EditProfile
                {
                    FirstName = customer.FirstName,
                    LastName = customer.LastName,
                    Email = customer.Email,
                    MobileNo = customer.MobilePhoneNo,
                    HousePhoneNo = customer.HousePhoneNo,
                    OfficePhoneNo = customer.OfficePhoneNo,
                    CustomerId = customer.Id
                };
                return View(editProfileModel);
            }
            return View();
        }

        [HttpPost]
        public ActionResult EditProfile(EditProfile model)
        {
            if (ModelState.IsValid)
            {
                Customer customer = customerService.GetLogInCustomerById(model.CustomerId);
                customer.FirstName = model.FirstName;
                customer.LastName = model.LastName;
                customer.MobilePhoneNo = model.MobileNo;
                customer.OfficePhoneNo = model.OfficePhoneNo;
                customer.HousePhoneNo = model.HousePhoneNo;
                customerService.UpdateCustomer(customer);
            }
            return View(model);

        }

        public ActionResult ChangePassword()
        {
            return View();
        }

        [HttpPost]
        public ActionResult ChangePassword(ChangePasswordModel model)
        {
            Customer customer = (Customer)Session[Constant.SESSION_CUSTOMER];
            if (ModelState.IsValid)
            {
                if (model.OldPassword == customer.Password)
                {
                    customer.Password = model.NewPassword;
                    customerService.UpdateCustomer(customer);
                }
                else
                {
                    ModelState.AddModelError("", "Your current password is wrong.");
                }
            }
            return View();
        }
    }
}
